<cfscript>
component extends="DynamoDBObject" {

	function init(accessKey, secretKey) {
		var creds = _jobj("com.amazonaws.auth.BasicAWSCredentials").init(accessKey, secretKey);
		variables.access = _jobj("com.amazonaws.services.dynamodb.AmazonDynamoDBAsyncClient").init(creds);
		variables.turnarounds = 0;

		super.init();
		return this;
	}
	
	function withEndpoint(endpoint) {
		variables.access.setEndpoint(endpoint);	   		
		return this;
	}

	function table(tableName) {
		return new Table(this, tableName);
	}
	
	function listTables() {
		local.request = _jobj("com.amazonaws.services.dynamodb.model.ListTablesRequest");
		local.response = doSync("listTables", local.request);
		return local.response.getTableNames();
	}	
	
	/* Private functions */

	package function turnarounds() {
		return variables.turnarounds;
	}

	package function doAsync(method, args) {
		++variables.turnarounds;
		return variables.access[method&"Async"](args);
	}
	
	package function doSync(method, args) {
		var result = doAsync(method, args).get();
		if (StructKeyExists(local, "result")) return local.result;
	}

}
</cfscript>
